/*
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
 
 Create at 10:51:02 AM on Mar 25, 2013.

 */
package com.google.srclib.data;

import java.sql.SQLException;
import java.util.Date;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.google.srclib.dataset.StockRecordDataset;

/**
 * @author lianyuy
 * 
 */
public class InvDatasetAdapter {

	public static InvDataset getData(StockRecordDataset sd) {
		TADataset daysDataset = new TADataset(DurationType.DAYS);
		InvDataset dataset = null;
		try {
			sd = adjustRecordDataset(sd);
			getDataByDays(sd, daysDataset);

			Date[] dates = daysDataset.getDateArray();
			double[] opens = daysDataset.getArray(TAKeyName.Open.getKey());
			double[] closes = daysDataset.getArray(TAKeyName.Close.getKey());
			double[] highs = daysDataset.getArray(TAKeyName.High.getKey());
			double[] lows = daysDataset.getArray(TAKeyName.Low.getKey());
			double[] volumns = daysDataset.getArray(TAKeyName.Volumn.getKey());

			dataset = new InvDataset(dates, opens, closes, highs, lows, volumns);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dataset;
	}

	private static StockRecordDataset adjustRecordDataset(StockRecordDataset sd)
			throws SQLException {
		if (sd.next()) {
			sd.beforeFirst();
			while (sd.next()) {
				double high = sd.getDouble(StockRecordDataset.HIGH);
				double low = sd.getDouble(StockRecordDataset.LOW);
				double open = sd.getDouble(StockRecordDataset.OPEN);
				double close = sd.getDouble(StockRecordDataset.CLOSE);
				double volumn = sd.getDouble(StockRecordDataset.VOLUMN);
				double adjclose = sd.getDouble(StockRecordDataset.ADJCLOSE);
				double ratio = adjclose / close;
				sd.updateDouble(StockRecordDataset.HIGH, high * ratio);
				sd.updateDouble(StockRecordDataset.LOW, low * ratio);
				sd.updateDouble(StockRecordDataset.OPEN, open * ratio);
				sd.updateDouble(StockRecordDataset.CLOSE, close * ratio);
				sd.updateDouble(StockRecordDataset.VOLUMN, volumn * ratio);
			}
			sd.beforeFirst();
		}
		return sd;
	}

	protected static void getDataByDays(StockRecordDataset data,
			TADataset taData) {
		DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
		try {
			data.beforeFirst();
			while (data.next()) {
				double high = data.getDouble(StockRecordDataset.HIGH);
				double low = data.getDouble(StockRecordDataset.LOW);
				double open = data.getDouble(StockRecordDataset.OPEN);
				double close = data.getDouble(StockRecordDataset.CLOSE);
				double volumn = data.getDouble(StockRecordDataset.VOLUMN);
				double adjclose = data.getDouble(StockRecordDataset.ADJCLOSE);
				taData.put(TAKeyName.High.getKey(), high);
				taData.put(TAKeyName.Low.getKey(), low);
				taData.put(TAKeyName.Open.getKey(), open);
				taData.put(TAKeyName.Close.getKey(), close);
				taData.put(TAKeyName.Volumn.getKey(), volumn);
				taData.put(TAKeyName.AdjClose.getKey(), adjclose);
				taData.put(fmt.parseLocalDate(data
						.getString(StockRecordDataset.DATE)));
			}
			data.beforeFirst();
		} catch (SQLException e) {

		}
	}

}
